<?php


namespace app\xiangji\controller;


use app\common\facades\OssUpload;
use app\Request;
use app\xiangji\BaseController;
use app\xiangji\services\FileService;
use think\App;
use think\exception\ValidateException;
use think\facade\Log;

class Upload extends BaseController
{
    const MAX_IMAGE_SIZE = 5 * 1024 * 1024; // 图片最大值为5M

    private $uploadService;

    public function __construct(FileService $uploadService)
    {
        $this->uploadService = $uploadService;
    }

    public function image(Request $request)
    {
        $image = $request->file('image');
        try {
            $this->validate(['image' => $image], ['image' => 'require|image|fileSize:'. self::MAX_IMAGE_SIZE]);
            $result = $this->uploadService->uploadImage($image);
            return $this->success($result);
        } catch (ValidateException $exception){
            return $this->failed($exception->getMessage());
        }
    }

    public function uploadTemp(Request $request)
    {
        $file = $request->file('file');
        try {
            $this->validate(['file' => $file], ['file' => 'require|fileSize:'. self::MAX_IMAGE_SIZE]);
            $result = $this->uploadService->uploadTemp($file);
            return $this->success($result);
        } catch (ValidateException $exception){
            return $this->failed($exception->getMessage());
        }
    }
}
